/**
 * Project Name:springmvc
 * File Name:LoginController.java
 * Package Name:com.pango.system.controller
 * Date:2014-5-26下午4:44:35
 * Copyright (c) 2014, fuqiongsong@gmail.com All Rights Reserved.
 *
*/

package com.pango.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pango.core.easyui.json.ResultMessage;
import com.pango.core.paginator.support.PageForm;
import com.pango.core.security.UserContext;
import com.pango.core.security.UserDetails;
import com.pango.core.utils.StringUtils;
import com.pango.system.entity.ResourceEntity;
import com.pango.system.model.Resource;
import com.pango.system.model.User;
import com.pango.system.services.ResourceService;
import com.pango.system.services.UserService;

/**
 * ClassName:LoginController <br/>
 * Date:     2014-5-26 下午4:44:35 <br/>
 * @author   thierry.fu
 * @version  
 * @since    JDK 1.6
 * @see 	 
 */
@Controller
@RequestMapping("/mainController")
public class MainController {
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private ResourceService resourceService;
	
    @RequestMapping(params = "main")
    public String login(HttpServletRequest req, Model model) {
        return "sys/main";
    }
    
    @RequestMapping(params = "main/left")
    public String left(Model model){
    	UserDetails user = UserContext.getCurrentUser();
    	List<ResourceEntity> resources = 
    			resourceService.selectResourcesByUserName(user.getUsername());

    	Map<String, List<Resource>> functions = new HashMap<String, List<Resource>>();
    	
    	//构建菜单层级
    	for (Resource resource : resources) {
			if(! functions.containsKey(resource.getLevel())) {
				functions.put(resource.getLevel(), new ArrayList<Resource>());
			}
			functions.get(resource.getLevel()).add(resource);
		}
    	
    	model.addAttribute("menuMap", functions);
    	return "sys/left";
    }
    
    @ResponseBody
    @RequestMapping(params = "user")
    public List<User> userView(User user,PageForm pageform) {
    	return userService.getAll(pageform.toPageBounds());
    }
    
    @RequestMapping(params = "list")
    public String userList() {
    	return "sys/list";
    }
    
    @RequestMapping(params = "addorupdate") 
    public String addorupdate(User user, HttpServletRequest req){
		if (StringUtils.isNotEmpty(user.getId())) {
			user = userService.findById(user.getId());
			req.setAttribute("userNote", user);
		}
		return "sys/userNote";
    }
    
    @ResponseBody
    @RequestMapping(params = "del") 
    public ResultMessage del(Long id) {
    	ResultMessage msg = new ResultMessage();
    	msg.setMsg("ajax");
    	return msg;
    }
    
    @ResponseBody
    @RequestMapping(params = "save") 
    public ResultMessage save(User user) {
    	ResultMessage msg = new ResultMessage();
    	userService.createUser(user);
    	msg.setMsg("ajax");
    	return msg;
    }
}
